package com.tianshi.product.controller;


import com.tianshi.entity.Categories;
import com.tianshi.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //通过id查询分类信息
    @GetMapping("/{categoryId}")
    public ResponseEntity<Categories> getCategoryById(@PathVariable("categoryId") Long id) {
        log.info("商品分类的id为："+id);
        Categories category = categoryService.getCategoryById(id);
//        log.info("查询到的商品分类信息为："+category);
        log.info("查询到的商品分类信息的id为："+category.getCategoryId());
        //System.out.println("商品分类信息为："+category);
        return ResponseEntity.ok(category);
    }
    //查询所有分类信息
    @GetMapping("/all")
    public ResponseEntity<List<Categories>> getAllCategory() {
        List<Categories> categories = categoryService.getAllCategory();
        log.info("查询到的所有商品分类信息为："+categories);
        return ResponseEntity.ok(categories);
    }
    //新增分类信息
    @PostMapping ("/add")
    public ResponseEntity<Categories> addCategory(@RequestBody Categories categories) {
        Long result = categoryService.addCategory(categories);
        log.info("新增的商品分类信息为："+categories);
        if (result > 0) {
            return ResponseEntity.ok(categories);
        }
        return ResponseEntity.noContent().build();
    }
    //修改分类信息
    @PutMapping ("/update")
    public ResponseEntity<String> updateCategory(@RequestBody Categories category) {
        Categories result = categoryService.updateCategory(category);
        log.info("修改的商品分类信息为："+category);
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        }
        return ResponseEntity.badRequest().body("修改失败");
    }
    //删除分类信息
    @DeleteMapping ("/delete/{categoryId}")
    public ResponseEntity<String> deleteCategory(@PathVariable("categoryId") Long id) {
        Long result = categoryService.deleteCategory(id);
        log.info("删除的商品分类id为："+id);
        if (result > 0) {
            return ResponseEntity.ok().body("删除成功");
        }
        return ResponseEntity.badRequest().body("删除失败");

    }

}
